main.R Markup

This program Performs a Prediction of MKT_TOTAL_VAL

First Header Second Header
File Name main.R
Author K. Rajesh Jagannath
Uses functions_MKT_VAL.R
Uses Assessors_yearly_data_ETL_MKT_VAL.R
Uses Buffer_parcels_ETL_MKT_VAL.R
Uses streetcarbuffer_parcels/gis_shape_files/StreetCar
Final Springboard Project submitted 4/30/2017
# Libraries used 
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.2.5
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
## Warning: package 'tidyr' was built under R version 3.2.5
library(ggplot2)
library(readr)
library(stringr)
library(scales)
## 
## Attaching package: 'scales'
## The following objects are masked from 'package:readr':
## 
##     col_factor, col_numeric
library(mixtools)
## mixtools package, version 1.0.4, Released 2016-01-11
## This package is based upon work supported by the National Science Foundation under Grant No. SES-0518772.
library(readxl)
library(lubridate)
## Warning: package 'lubridate' was built under R version 3.2.5
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
library(reshape2)
## 
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
## 
##     smiths
library(forecast)
## Warning: package 'forecast' was built under R version 3.2.5
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.2.5
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: timeDate
## This is forecast 7.2
library(TSclust)
## Loading required package: wmtsa
## Warning: package 'wmtsa' was built under R version 3.2.5
## Loading required package: pdc
## Loading required package: cluster
## Warning: package 'cluster' was built under R version 3.2.5
library(sqldf)
## Loading required package: gsubfn
## Loading required package: proto
## Loading required package: RSQLite
## Loading required package: DBI
## Warning: package 'DBI' was built under R version 3.2.5
library(ClustOfVar)
library(digest)
library(zoo)
library(maptools)
## Loading required package: sp
## Warning: package 'sp' was built under R version 3.2.5
## Checking rgeos availability: FALSE
##      Note: when rgeos is not available, polygon geometry     computations in maptools depend on gpclib,
##      which has a restricted licence. It is disabled by default;
##      to enable gpclib, type gpclibPermit()
library(rgdal)
## Warning: package 'rgdal' was built under R version 3.2.5
## rgdal: version: 1.1-10, (SVN revision 622)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 1.11.4, released 2016/01/25
##  Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.2/Resources/library/rgdal/gdal
##  Loaded PROJ.4 runtime: Rel. 4.9.1, 04 March 2015, [PJ_VERSION: 491]
##  Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.2/Resources/library/rgdal/proj
##  Linking to sp version: 1.2-3
# set the sed for repeatability


set.seed(100)

options(scipen=12)

# Set variables 

#
# Analysis is done on non_buffer_zone as well as CORE, CENTER and EDGE buffer zones
# set skip_non_buffer = 1 to skip directly to CORE, CENTER and EDGE analysis
# used for bring up and debug only
skip_non_buffer = 0
#
# Choose a smaller sample  for non buffer zone : skip_1500 = 1 for dev
# Choose about 1500 samples don't skip : for analysis
# used for debug and bring up only
skip_1500 = 0
print(" Setting current working directory ")
## [1] " Setting current working directory "
# set the working dir
setwd("/Users/rajesh/Desktop/Coursera/SpringBoardGithub/Streetcar0719")
print(" sourcing functions ")
## [1] " sourcing functions "
setwd("/Users/rajesh/Desktop/Coursera/SpringBoardGithub/Streetcar0719/taxinfoTimeSeries")
source ('/Users/rajesh/Desktop/Coursera/SpringBoardGithub/Streetcar0719/taxinfoTimeSeries/functions_MKT_VAL.R')

 setwd("/Users/rajesh/Desktop/Coursera/SpringBoardGithub/Streetcar0719")






print( "Reading in  8 years of parcel data 1/4 Million observations X 8 years")
## [1] "Reading in  8 years of parcel data 1/4 Million observations X 8 years"
print(" Performing Extraction, Transformation and Loading ... ")
## [1] " Performing Extraction, Transformation and Loading ... "
# Here we read in 8 years data for 250,000 - 275,000 parcels for all of Cincinnati
# Extraction Transformation and Cleaning is done here
source('taxinfoTimeSeries/Assessors_yearly_data_ETL_MKT_VAL.R')
## [1] "Processing the year : 2007"
## [1] "df_taxinfo_2007"
## [1] "Processing the year : 2008"
## [1] "df_taxinfo_2008"
## [1] "Processing the year : 2009"
## [1] "df_taxinfo_2009"
## [1] "Processing the year : 2010"
## [1] "df_taxinfo_2010"
## [1] "Processing the year : 2011"
## [1] "df_taxinfo_2011"
## [1] "Processing the year : 2012"
## [1] "df_taxinfo_2012"
## [1] "Processing the year : 2013"
## [1] "df_taxinfo_2013"
## [1] "Processing the year : 2014"
## [1] "df_taxinfo_2014"
## [1] "Processing the year : 2015"
## [1] "df_taxinfo_2015"
## [1] "processing df_taxinfo_2007$MKT_LAND_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2007$MKT_IMPR_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2007$MKT_TOTAL_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2007$ANNUAL_TAXES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2007$TAXES_PAID"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2007$DELQ_TAXES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2007$ACRES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2007$SALE_AMOUNT"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2007$NEW_CONS_FLAG"
## [1] "processing df_taxinfo_2007$FORECL_FLAG"
## [1] "processing df_taxinfo_2008$MKT_LAND_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2008$MKT_IMPR_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2008$MKT_TOTAL_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2008$ANNUAL_TAXES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2008$TAXES_PAID"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2008$DELQ_TAXES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2008$ACRES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2008$SALE_AMOUNT"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2008$NEW_CONS_FLAG"
## [1] "processing df_taxinfo_2008$FORECL_FLAG"
## [1] "processing df_taxinfo_2009$MKT_LAND_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2009$MKT_IMPR_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2009$MKT_TOTAL_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2009$ANNUAL_TAXES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2009$TAXES_PAID"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2009$DELQ_TAXES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2009$ACRES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2009$SALE_AMOUNT"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2009$NEW_CONS_FLAG"
## [1] "processing df_taxinfo_2009$FORECL_FLAG"
## [1] "processing df_taxinfo_2010$MKT_LAND_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2010$MKT_IMPR_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2010$MKT_TOTAL_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2010$ANNUAL_TAXES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2010$TAXES_PAID"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2010$DELQ_TAXES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2010$ACRES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2010$SALE_AMOUNT"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2010$NEW_CONS_FLAG"
## [1] "processing df_taxinfo_2010$FORECL_FLAG"
## [1] "processing df_taxinfo_2011$MKT_LAND_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2011$MKT_IMPR_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2011$MKT_TOTAL_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2011$ANNUAL_TAXES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2011$TAXES_PAID"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2011$DELQ_TAXES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2011$ACRES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2011$SALE_AMOUNT"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2011$NEW_CONS_FLAG"
## [1] "processing df_taxinfo_2011$FORECL_FLAG"
## [1] "processing df_taxinfo_2012$MKT_LAND_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2012$MKT_IMPR_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2012$MKT_TOTAL_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2012$ANNUAL_TAXES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2012$TAXES_PAID"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2012$DELQ_TAXES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2012$ACRES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2012$SALE_AMOUNT"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2012$NEW_CONS_FLAG"
## [1] "processing df_taxinfo_2012$FORECL_FLAG"
## [1] "processing df_taxinfo_2013$MKT_LAND_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2013$MKT_IMPR_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2013$MKT_TOTAL_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2013$ANNUAL_TAXES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2013$TAXES_PAID"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2013$DELQ_TAXES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2013$ACRES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2013$SALE_AMOUNT"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2013$NEW_CONS_FLAG"
## [1] "processing df_taxinfo_2013$FORECL_FLAG"
## [1] "processing df_taxinfo_2014$MKT_LAND_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2014$MKT_IMPR_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2014$MKT_TOTAL_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2014$ANNUAL_TAXES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2014$TAXES_PAID"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2014$DELQ_TAXES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2014$ACRES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2014$SALE_AMOUNT"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2014$NEW_CONS_FLAG"
## [1] "processing df_taxinfo_2014$FORECL_FLAG"
## [1] "processing df_taxinfo_2015$MKT_LAND_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2015$MKT_IMPR_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2015$MKT_TOTAL_VAL"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2015$ANNUAL_TAXES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2015$TAXES_PAID"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2015$DELQ_TAXES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2015$ACRES"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2015$SALE_AMOUNT"
## Warning: NAs introduced by coercion
## [1] "processing df_taxinfo_2015$NEW_CONS_FLAG"
## [1] "processing df_taxinfo_2015$FORECL_FLAG"
## Warning: NAs introduced by coercion
## Warning: NAs introduced by coercion

## Warning: NAs introduced by coercion

## Warning: NAs introduced by coercion

## Warning: NAs introduced by coercion

## Warning: NAs introduced by coercion

## Warning: NAs introduced by coercion

## Warning: NAs introduced by coercion

## Warning: NAs introduced by coercion

## Warning: NAs introduced by coercion

## Warning: NAs introduced by coercion

## Warning: NAs introduced by coercion
## Warning: Setting row names on a tibble is deprecated.
print( "Reading Buffer zone Parcel information ")
## [1] "Reading Buffer zone Parcel information "
print(" Performing Extraction, Transformation and Loading ... ")
## [1] " Performing Extraction, Transformation and Loading ... "
# here we read in and clean up the Parcels of the Buffer
# extraction , Transormaton and Cleaning is done in this file
source('taxinfoTimeSeries/Buffer_parcels_ETL_MKT_VAL.R')
## [1] "Processing the zone : CENTER"
## [1] "df_buffer_CENTER"
## [1] "Processing the zone : EDGE"
## [1] "df_buffer_EDGE"
## [1] "Processing the zone : CORE"
## [1] "df_buffer_CORE"
setwd("/Users/rajesh/Desktop/Coursera/SpringBoardGithub/Streetcar0719")
if(skip_non_buffer == 0) {

  
  print(" NON BUFFER ZONE : Hierarchical Clustering ")
  # Begin Hierarchical Clustering of parcel ids
  
  # For illustartive purposes we choose first 10 columns
  Clustvar <-hclustvar(taxinfo.matrix_no_na[, 1:10])
  plot(Clustvar)
  
  print(" NON BUFFER ZONE : 100 random samples  Hierarchical Clustering ")
  
  #then random 100 columns
  n <- ncol(taxinfo.matrix_no_na)
  taxinfo.matrix_shuffled <- taxinfo.matrix_no_na[,sample(n) ]
  #0.01248  is 2500 values
  train_indices <- 1:round(0.00042226 * n)
  train <- taxinfo.matrix_shuffled[,train_indices ]
  
  
  Clustvar100 <-hclustvar(train)
  plot(Clustvar100)
  rect.hclust(Clustvar100,k=12, border="red")
  part100 <- cutreevar(Clustvar100,12)
  
  plot(Clustvar100$height)
  
  print(" NON BUFFER ZONE : 100 random samples  Stability graph ")
  
  
  
  #stab100 <- stability(Clustvar100,B = 60, graph = TRUE)
  
  if(skip_1500 == 0) {
    #Now let's get some  random PARCEL_IDs
    # About 1500 should do
    
    n <- ncol(taxinfo.matrix_no_na)
    taxinfo.matrix_shuffled <- taxinfo.matrix_no_na[,sample(n) ]
    #0.0051699 of 241748 observations is 1249
    train_indices <- 1:round(0.0051699 * n)
    train <- taxinfo.matrix_shuffled[,train_indices ]
    
    
    
    Clustvar1500 <-hclustvar(train)
    plot(Clustvar1500)
    rect.hclust(Clustvar1500,k=12, border="red")
    
    
    #####
    # B is the Boot strapping sample and we determine the stability
    # Evaluates the stability of partitions obtained from a hierarchy of p variables. This hierarchy is performed
    # with hclustvar and the stability of the partitions of 2 to p-1 clusters is evaluated with a
    # bootstrap approach. The boostrap approch is the following: hclustvar is applied to B bootstrap
    # samples of the n rows. The partitions of 2 to p-1 clusters obtained from the B bootstrap hierarchies
    # are compared with the partitions from the initial hierarchy . The mean of the corrected Rand
    # indices is plotted according to the number of clusters. This graphical representation helps in the
    # determination of a suitable numbers of clusters.
    
    #stab1500 <- stability(Clustvar1500,B = 5, graph = TRUE)
    
    
    # Could not get a graph : Make a rough partition of 12 clusters
    
    part1500 <- cutreevar(Clustvar1500,12)
    plot(Clustvar1500$height)
    
    a <- part1500$var
  } else {
    part100 <- cutreevar(Clustvar100,12)
    plot(Clustvar100$height)
    
    a <- part100$var
    
  }
  #Remove xs from Parcel ids
  
  
  train_cl1  <- as.data.frame(row.names(a$cluster1), stringsAsFactors = FALSE)
  train_cl2  <- as.data.frame(row.names(a$cluster2), stringsAsFactors = FALSE)
  train_cl3  <- as.data.frame(row.names(a$cluster3), stringsAsFactors = FALSE)
  train_cl4  <- as.data.frame(row.names(a$cluster4), stringsAsFactors = FALSE)
  train_cl5  <- as.data.frame(row.names(a$cluster5), stringsAsFactors = FALSE)
  train_cl6  <- as.data.frame(row.names(a$cluster6), stringsAsFactors = FALSE)
  train_cl7  <- as.data.frame(row.names(a$cluster7), stringsAsFactors = FALSE)
  train_cl8  <- as.data.frame(row.names(a$cluster8), stringsAsFactors = FALSE)
  train_cl9  <- as.data.frame(row.names(a$cluster9), stringsAsFactors = FALSE)
  train_cl10 <- as.data.frame(row.names(a$cluster10), stringsAsFactors = FALSE)
  train_cl11 <- as.data.frame(row.names(a$cluster11), stringsAsFactors = FALSE)
  train_cl12 <- as.data.frame(row.names(a$cluster12), stringsAsFactors = FALSE)
  
  
  # Remove X's from the PARCEL IDs
  train_cl1  <- apply(train_cl1,2, function(y) as.character(gsub("X", "", y)))
  train_cl2  <- apply(train_cl2,2, function(y) as.character(gsub("X", "", y)))
  train_cl3  <- apply(train_cl3,2, function(y) as.character(gsub("X", "", y)))
  train_cl4  <- apply(train_cl4,2, function(y) as.character(gsub("X", "", y)))
  train_cl5  <- apply(train_cl5,2, function(y) as.character(gsub("X", "", y)))
  train_cl6  <- apply(train_cl6,2, function(y) as.character(gsub("X", "", y)))
  train_cl7  <- apply(train_cl7,2, function(y) as.character(gsub("X", "", y)))
  train_cl8  <- apply(train_cl8,2, function(y) as.character(gsub("X", "", y)))
  train_cl9  <- apply(train_cl9,2, function(y) as.character(gsub("X", "", y)))
  train_cl10 <- apply(train_cl10,2, function(y) as.character(gsub("X", "", y)))
  train_cl11 <- apply(train_cl11,2, function(y) as.character(gsub("X", "", y)))
  train_cl12 <- apply(train_cl12,2, function(y) as.character(gsub("X", "", y)))
  
  
  
  
  # Use Holt-Winters  for Simple Exponential Smoothing
  for ( clst in c(seq(1:12))) {
    forecast_clusters(clst,a, taxinfo.matrix_uniq)
    
  }
  
  
  
  
  # we create 5 models of regressions
  # 1st order - a + T              ( linear)
  # 2nd order a + bT + cT^2        ( quadratic)
  # 3rd order a = bT + cT^2 + dT^3 ( cubic)
  # 4th order and 
  # 5th order as well 
  
  print("NON BUFFER ZONE : Plotting Actuals vs. Fitted for models in clusters")
  print("NON BUFFER ZONE : Evaluating 12 Clusters with 1st, 2nd, 3rd and 4th and 5th order polynomial regression")
  print("NON BUFFER ZONE : Years 2007-2014 serve as training data")
  print("NON BUFFER ZONE : Years 2015-2018 serve as test data for validation")
  
  
  for( poly_order in c(seq(1:5))){
    
    for ( clst in c(seq(1:12))) {
      
      df_mape_train_clusters <- plot_act_pred_trg(clst,a, taxinfo.matrix[c(1:6),], poly_order)
      df_mape_test_clusters  <- plot_act_pred_test(clst,a, taxinfo.matrix[c(1:6),], taxinfo.matrix[c(7:8),], poly_order)
      
      if (clst == 1) {
        # df_mape_train_clst_poly <- df_mape_train_clusters["mape"]
        df_actpred_train_clst_poly <- df_mape_train_clusters["act_pred"]
        df_mape_test_clst_poly <- df_mape_test_clusters["mape"]
        
        
      }
      else {
        
        
        #df_mape_train_clst_poly <- bind_rows(df_mape_train_clst_poly, df_mape_train_clusters["mape"])
        df_actpred_train_clst_poly <- bind_rows(df_actpred_train_clst_poly, df_mape_train_clusters["act_pred"])
        
        
        df_mape_test_clst_poly  <- bind_rows(df_mape_test_clst_poly,  df_mape_test_clusters["mape"])
      }
    }
    
    if (poly_order == 1) {
      #df_mape_train_all <- df_mape_train_clst_poly
      df_actpred_train_all <- df_actpred_train_clst_poly
      
      
      df_mape_test_all  <- df_mape_test_clst_poly
      
    } 
    else 
    {
      #df_mape_train_all <- bind_rows(df_mape_train_all, df_mape_train_clst_poly) 
      df_actpred_train_all <- bind_rows(df_actpred_train_all, df_actpred_train_clst_poly) 
      
      df_mape_test_all <- bind_rows(df_mape_test_all, df_mape_test_clst_poly) 
      
      
    }
    
    
    
    
  }
  
  print("NON BUFFER ZONE : Computing MAPE - Mean Absolute Percentage error")
  
  # Chart the actuals and predicted for the different clusters
  
  print("NON BUFFER ZONE: Plotting the Actual vs. Fitted for Training set ")
  
  
  for( poly_order in c(seq(1:5))){
    
    plot_actpred_chart(df_actpred_train_all, " Non BUFFER ZONE training set ", poly_order)
    
    
  } # poly_order
  
  
  # Evaluate the Mean Absolute Percentage error(MAPE)
  # Training data
  # Years 2007-2012
  #plot_mape_chart(df_mape_train_all, "non Buffer zone : training data")
  
  # Evaluate the MAPE for 
  # Testing data 
  # years 2013-2014
  
  plot_mape_chart(df_mape_test_all, "non Buffer zone : test data")
  
  print("NON BUFFER ZONE :  Plotting MKT_TOT_VAL source and predicted data")
  print("NON BUFFER ZONE :  Creating df_final_mkt_val data frame for later Visualization")
  
  
  for( poly_order in c(seq(1:5))){
    
    # facet the clusters on a single plot 
    # visualize  fitted and predicted
    
    df_final_mkt_val_non_bfr_zone <- non_linear_regresion_model(a, taxinfo.matrix, poly_order, 12, "non-Buffer", df_mape_test_all[(df_mape_test_all$poly_order < 4),])
    if ( poly_order == 1) {
      df_final_mkt_val <- df_final_mkt_val_non_bfr_zone 
    } 
    else{ df_final_mkt_val <- bind_rows(df_final_mkt_val, df_final_mkt_val_non_bfr_zone )}
    
    
  }
  
} # skip_non_buffer_zone
## [1] " NON BUFFER ZONE : Hierarchical Clustering "

## [1] " NON BUFFER ZONE : 100 random samples  Hierarchical Clustering "

## [1] " NON BUFFER ZONE : 100 random samples  Stability graph "

## [1] "NON BUFFER ZONE : Plotting Actuals vs. Fitted for models in clusters"
## [1] "NON BUFFER ZONE : Evaluating 12 Clusters with 1st, 2nd, 3rd and 4th and 5th order polynomial regression"
## [1] "NON BUFFER ZONE : Years 2007-2014 serve as training data"
## [1] "NON BUFFER ZONE : Years 2015-2018 serve as test data for validation"
## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## [1] "NON BUFFER ZONE : Computing MAPE - Mean Absolute Percentage error"
## [1] "NON BUFFER ZONE: Plotting the Actual vs. Fitted for Training set "

## [1] "NON BUFFER ZONE :  Plotting MKT_TOT_VAL source and predicted data"
## [1] "NON BUFFER ZONE :  Creating df_final_mkt_val data frame for later Visualization"

## Warning: Stacking not well defined when ymin != 0

## Warning: Stacking not well defined when ymin != 0

print("BUFFER ZONE: Training and test data set ")
## [1] "BUFFER ZONE: Training and test data set "
######
# EDGE
######

print("EDGE BUFFER ZONE :  Training Data - Dendogram")
## [1] "EDGE BUFFER ZONE :  Training Data - Dendogram"
#then random 100 columns
n <- ncol(taxinfo_EDGE.matrix_no_na)
taxinfo_EDGE.matrix_shuffled <- taxinfo_EDGE.matrix_no_na[,sample(n) ]
#0.7   is 813 values
train_indices <- 1:round(0.7 * n)
train <- taxinfo_EDGE.matrix_shuffled[,train_indices ]


ClustvarEDGE100 <-hclustvar(train)
plot(ClustvarEDGE100)
rect.hclust(ClustvarEDGE100,k=12, border="red")

partEDGE100 <- cutreevar(ClustvarEDGE100,12)


a_EDGE <- partEDGE100$var

plot(ClustvarEDGE100$height)

######
# CENTER
######
print("CENTER BUFFER ZONE :  Training Data - Dendogram")
## [1] "CENTER BUFFER ZONE :  Training Data - Dendogram"
#then random 100 columns
n <- ncol(taxinfo_CENTER.matrix_no_na)
taxinfo_CENTER.matrix_shuffled <- taxinfo_CENTER.matrix_no_na[,sample(n) ]
#0.7   is 561 values
train_indices <- 1:round(0.7 * n)
train <- taxinfo_CENTER.matrix_shuffled[,train_indices ]


ClustvarCENTER100 <-hclustvar(train)
plot(ClustvarCENTER100)
rect.hclust(ClustvarCENTER100,k=12, border="red")

partCENTER100 <- cutreevar(ClustvarCENTER100,12)
a_CENTER <- partCENTER100$var
plot(ClustvarCENTER100$height)

#stab100 <- stability(Clustvar100,B = 60, graph = TRUE)

######
# CORE
######

print("CORE BUFFER ZONE :  Training Data - Dendogram")
## [1] "CORE BUFFER ZONE :  Training Data - Dendogram"
#then random 100 columns
n <- ncol(taxinfo_CORE.matrix_no_na)
taxinfo_CORE.matrix_shuffled <- taxinfo_CORE.matrix_no_na[,sample(n) ]
#0.7   is 561 values
train_indices <- 1:round(0.7 * n)
train <- taxinfo_CORE.matrix_shuffled[,train_indices ]


ClustvarCORE100 <-hclustvar(train)
plot(ClustvarCORE100)
rect.hclust(ClustvarCORE100,k=12, border="red")

partCORE100 <- cutreevar(ClustvarCORE100,12)

a_CORE <-partCORE100$var

plot(ClustvarCORE100$height)

#stab100 <- stability(Clustvar100,B = 60, graph = TRUE)

print("BUFFER_ZONE: About to plot actuals and fitted")
## [1] "BUFFER_ZONE: About to plot actuals and fitted"
for( poly_order in c(seq(1:5))){
  
  
  for ( clst in c(seq(1:12))) {
    
    
    
    df_mape_train_CORE_parcels   <- plot_act_pred_trg(clst,a_CORE,  taxinfo_CORE.matrix[c(1:6),], poly_order)
    df_mape_train_CENTER_parcels <- plot_act_pred_trg(clst,a_CENTER,  taxinfo_CENTER.matrix[c(1:6),], poly_order)
    df_mape_train_EDGE_parcels   <- plot_act_pred_trg(clst,a_EDGE,  taxinfo_EDGE.matrix[c(1:6),], poly_order)
    
    
    
    
    df_mape_test_CORE_parcels   <- plot_act_pred_test(clst,a_CORE,  taxinfo_CORE.matrix[c(1:6),], taxinfo_CORE.matrix[c(7:8),],    poly_order)
    df_mape_test_CENTER_parcels <- plot_act_pred_test(clst,a_CENTER,  taxinfo_CENTER.matrix[c(1:6),],taxinfo_CENTER.matrix[c(7:8),], poly_order)
    df_mape_test_EDGE_parcels   <- plot_act_pred_test(clst,a_EDGE,  taxinfo_EDGE.matrix[c(1:6),],taxinfo_EDGE.matrix[c(7:8),], poly_order)
    
    
    
    
    if (clst == 1) {
      
      df_mape_train_CORE_clst_poly <- df_mape_train_CORE_parcels["mape"]
      df_mape_train_CENTER_clst_poly <- df_mape_train_CENTER_parcels["mape"]
      df_mape_train_EDGE_clst_poly <- df_mape_train_EDGE_parcels["mape"]
      
      df_actpred_train_CORE_clst_poly <- df_mape_train_CORE_parcels["act_pred"]
      df_actpred_train_CENTER_clst_poly <- df_mape_train_CENTER_parcels["act_pred"]
      df_actpred_train_EDGE_clst_poly <- df_mape_train_EDGE_parcels["act_pred"]      
      
      
      
      df_mape_test_CORE_clst_poly <- df_mape_test_CORE_parcels["mape"]
      df_mape_test_CENTER_clst_poly <- df_mape_test_CENTER_parcels["mape"]
      df_mape_test_EDGE_clst_poly <- df_mape_test_EDGE_parcels["mape"]
      
      
      
      
      
      
    }
    else {
      
      #df_mape_train_CORE_clst_poly <- bind_rows(df_mape_train_CORE_clst_poly,     df_mape_train_CORE_parcels["mape"])
      #df_mape_train_CENTER_clst_poly <- bind_rows(df_mape_train_CENTER_clst_poly,     df_mape_train_CENTER_parcels["mape"])
      #df_mape_train_EDGE_clst_poly <- bind_rows(df_mape_train_EDGE_clst_poly,     df_mape_train_EDGE_parcels["mape"])
      
      
      
      df_actpred_train_CORE_clst_poly <- bind_rows(df_actpred_train_CORE_clst_poly,     df_mape_train_CORE_parcels["act_pred"])
      df_actpred_train_CENTER_clst_poly <- bind_rows(df_actpred_train_CENTER_clst_poly, df_mape_train_CENTER_parcels["act_pred"])
      df_actpred_train_EDGE_clst_poly <- bind_rows(df_actpred_train_EDGE_clst_poly,     df_mape_train_EDGE_parcels["act_pred"])
      
      
      
      df_mape_test_CORE_clst_poly <- bind_rows(df_mape_test_CORE_clst_poly,     df_mape_test_CORE_parcels["mape"])
      df_mape_test_CENTER_clst_poly <- bind_rows(df_mape_test_CENTER_clst_poly,     df_mape_test_CENTER_parcels["mape"])
      df_mape_test_EDGE_clst_poly <- bind_rows(df_mape_test_EDGE_clst_poly,     df_mape_test_EDGE_parcels["mape"])
      
      
      
    }
  }
  
  
  if (poly_order == 1) {
    
    
    #df_mape_train_CORE_all   <- df_mape_train_CORE_clst_poly
    #df_mape_train_CENTER_all <- df_mape_train_CENTER_clst_poly
    #df_mape_train_EDGE_all   <- df_mape_train_EDGE_clst_poly 
    
    df_actpred_train_CORE_all   <- df_actpred_train_CORE_clst_poly
    df_actpred_train_CENTER_all <- df_actpred_train_CENTER_clst_poly
    df_actpred_train_EDGE_all   <- df_actpred_train_EDGE_clst_poly 
    
    
    
    
    
    
    df_mape_test_CORE_all   <- df_mape_test_CORE_clst_poly
    df_mape_test_CENTER_all <- df_mape_test_CENTER_clst_poly
    df_mape_test_EDGE_all   <- df_mape_test_EDGE_clst_poly 
    
    
    
  } 
  else 
  {
    
    # df_mape_train_CORE_all <- bind_rows(df_mape_train_CORE_all, df_mape_train_CORE_clst_poly) 
    # df_mape_train_CENTER_all <- bind_rows(df_mape_train_CENTER_all, df_mape_train_CENTER_clst_poly) 
    # df_mape_train_EDGE_all <- bind_rows(df_mape_train_EDGE_all, df_mape_train_EDGE_clst_poly)  
    
    df_actpred_train_CORE_all <- bind_rows(df_actpred_train_CORE_all, df_actpred_train_CORE_clst_poly) 
    df_actpred_train_CENTER_all <- bind_rows(df_actpred_train_CENTER_all, df_actpred_train_CENTER_clst_poly) 
    df_actpred_train_EDGE_all <- bind_rows(df_actpred_train_EDGE_all, df_actpred_train_EDGE_clst_poly)  
    
    
    
    
    df_mape_test_CORE_all <- bind_rows(df_mape_test_CORE_all, df_mape_test_CORE_clst_poly) 
    df_mape_test_CENTER_all <- bind_rows(df_mape_test_CENTER_all, df_mape_test_CENTER_clst_poly) 
    df_mape_test_EDGE_all <- bind_rows(df_mape_test_EDGE_all, df_mape_test_EDGE_clst_poly)   
    
    
    
    
  }
  
  
  
}
## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed

## Warning in window.default(x, ...): 'end' value not changed
# print("BUFFER ZONE: Plotting the Actual vs. Fitted for Training set ")
# 
# 
# for( poly_order in c(seq(1:5))){
#   
#   plot_actpred_chart(df_actpred_train_CORE_all, " CORE BUFFER ZONE training set ",poly_order)
#   plot_actpred_chart(df_actpred_train_CENTER_all, " CENTER BUFFER ZONE training set", poly_order)
#   plot_actpred_chart(df_actpred_train_EDGE_all, " EDGE BUFFER ZONE training set ", poly_order)
#   
# } # poly_order

print("BUFFER ZONE: Plotting the mape charts ")
## [1] "BUFFER ZONE: Plotting the mape charts "
# Plot the mape chart for the buffer zones


# plot_mape_chart(df_mape_train_CORE_all, " CORE BUFFER ZONE training set data")
# plot_mape_chart(df_mape_train_CENTER_all, " CENTER BUFFER ZONE training set  data")
# plot_mape_chart(df_mape_train_EDGE_all, " EDGE BUFFER ZONE training set data")
# 



plot_mape_chart(df_mape_test_CORE_all, " CORE BUFFER ZONE test set data")

plot_mape_chart(df_mape_test_CENTER_all, " CENTER BUFFER ZONE test set  data")

plot_mape_chart(df_mape_test_EDGE_all, " EDGE BUFFER ZONE test set data")

# Write out to csv files
write.csv(df_mape_test_CORE_all, file = "test_set_mape_CORE.csv" )
write.csv(df_mape_test_CENTER_all, file = "test_set_mape_CENTER.csv" )
write.csv(df_mape_test_EDGE_all, file = "test_set_mape_EDGE.csv" )

print( "BUFFER_ZONE : Faceting over  clusters for each zone")
## [1] "BUFFER_ZONE : Faceting over  clusters for each zone"
# facet the clusters on a single plot 
# visualize  fitted and predicted


######
# EDGE
######

# here  all of the  Buffer zone parcel ids are partitioned into clusters

# Find the number of observations
n <- ncol(taxinfo_EDGE.matrix_no_na)
# Shuffle it
ntaxinfo_EDGE.matrix_shuffled <- taxinfo_EDGE.matrix_no_na[,sample(n) ]
train_indices <- 1:round(1.0 * n)
train <- taxinfo_EDGE.matrix_shuffled[,train_indices ]

# perform Clurtering of Vars / dimension reduction
ClustvarEDGE100 <-hclustvar(train)
# plot the dendogram
plot(ClustvarEDGE100)
rect.hclust(ClustvarEDGE100,k=12, border="red")

partEDGE100 <- cutreevar(ClustvarEDGE100,12)

a_EDGE <- partEDGE100$var
# Height of ClustVarEDGE is plotted
plot(ClustvarEDGE100$height)

######
# CENTER
######


# Find the number of observations and shuffle it
n <- ncol(taxinfo_CENTER.matrix_no_na)
taxinfo_CENTER.matrix_shuffled <- taxinfo_CENTER.matrix_no_na[,sample(n) ]
#Take all of the observations
train_indices <- 1:round(1.0 * n)
train <- taxinfo_CENTER.matrix_shuffled[,train_indices ]

# create a dendogram
ClustvarCENTER100 <-hclustvar(train)
plot(ClustvarCENTER100)
rect.hclust(ClustvarCENTER100,k=12, border="red")

partCENTER100 <- cutreevar(ClustvarCENTER100,12)

a_CENTER <- partCENTER100$var

# plot the height
plot(ClustvarCENTER100$height)

######
# CORE
######


#Take all of the observations and shuffle them
n <- ncol(taxinfo_CORE.matrix_no_na)
taxinfo_CORE.matrix_shuffled <- taxinfo_CORE.matrix_no_na[,sample(n) ]

train_indices <- 1:round(1.0 * n)
train <- taxinfo_CORE.matrix_shuffled[,train_indices ]

# Perform Dimension reduction 
ClustvarCORE100 <-hclustvar(train)
plot(ClustvarCORE100)
partCORE100 <- cutreevar(ClustvarCORE100,12)
rect.hclust(ClustvarCORE100,k=12, border="red")

a_CORE <-partCORE100$var

plot(ClustvarCORE100$height)

#stab100 <- stability(Clustvar100,B = 60, graph = TRUE)
print("BUFFER_ZONE Completed dendograms for all the parcels")
## [1] "BUFFER_ZONE Completed dendograms for all the parcels"
print("BUFFER_ZONE - Source Data and Predicted in the same plot")
## [1] "BUFFER_ZONE - Source Data and Predicted in the same plot"
for( poly_order in c(seq(1:5))){
  
  # facet the clusters on a single plot 
  # visualize  source and predicted
  
  df_final_mkt_val_core <- non_linear_regresion_model(a_CORE, taxinfo_CORE.matrix, poly_order,12, "CORE", df_mape_test_CORE_all[(df_mape_test_CORE_all$poly_order <4),])
  if (( poly_order == 1) && (skip_non_buffer == 1)) {
    df_final_mkt_val <- df_final_mkt_val_core 
  } else
  { df_final_mkt_val <- bind_rows(df_final_mkt_val, df_final_mkt_val_core )}
}

## Warning: Stacking not well defined when ymin != 0

## Warning: Stacking not well defined when ymin != 0

## Warning: Stacking not well defined when ymin != 0

for( poly_order in c(seq(1:5))){
  
  # facet the clusters on a single plot 
  # visualize  source and predicted
  
  df_final_mkt_val_center <- non_linear_regresion_model(a_CENTER, taxinfo_CENTER.matrix, poly_order,12, "CENTER",df_mape_test_CENTER_all[(df_mape_test_CENTER_all$poly_order <4),])
  df_final_mkt_val <- bind_rows(df_final_mkt_val, df_final_mkt_val_center )
}

## Warning: Stacking not well defined when ymin != 0

## Warning: Stacking not well defined when ymin != 0

## Warning: Stacking not well defined when ymin != 0

for( poly_order in c(seq(1:5))){
  
  # facet the clusters on a single plot 
  # visualize  source and predicted
  df_final_mkt_val_edge <- non_linear_regresion_model(a_EDGE, taxinfo_EDGE.matrix, poly_order,12, "EDGE", df_mape_test_EDGE_all[(df_mape_test_EDGE_all$poly_order <4),])
  df_final_mkt_val <- bind_rows(df_final_mkt_val, df_final_mkt_val_edge )
  
}

## Warning: Stacking not well defined when ymin != 0

## Warning: Stacking not well defined when ymin != 0

## Warning: Stacking not well defined when ymin != 0

# For the plots below we need to make the zone a factor
df_final_mkt_val$zone <- as.factor(df_final_mkt_val$zone)
df_final_mkt_val$data_type <- as.factor(df_final_mkt_val$data_type)
df_final_mkt_val$date <- as.factor(df_final_mkt_val$date)


# Also find the mkt_total/ cluster = ( mkt_val_mean/cluster  * num_parcel_ids/cluster)
df_final_mkt_val <- df_final_mkt_val %>% mutate(mkt_total = mkt_val * num_parcel_ids)

# Write out to csv files
write.csv(df_final_mkt_val, file = "final_mkt_val.csv" )



print(" ZONE UNDER STUDY : Final plot ")
## [1] " ZONE UNDER STUDY : Final plot "
n3 <- NULL
# Data Layer
#title <- paste0("MKT TOTAL VALUE  in different ZONEs ")

title <- paste0("MKT TOTAL VALUE in different ZONEs ")


n3 <-(ggplot)
# 
# # Data + Aesthetics Mapping 
#n3 <- ggplot(df_final_mkt_val,aes(y = mkt_total, x = factor(date), fill = factor(clst_num), color=factor(data_type)))
n3 <- ggplot(df_final_mkt_val,aes(y = mkt_val, x = factor(date), fill = factor(clst_num), color=factor(data_type)))

#n3 <-   ggplot(df_final_mkt_val,aes(y = mkt_val, x = date ))
# 
# # Data + aes + Geometries
n3 <- n3 + geom_bar(stat="identity", width = 0.6) 
#n3 <- n3 + geom_line()
# 
# # Data + Aesthetic Mapping +  Geom + Facets
# #n <- n + facet_grid(. ~ Department.Title ) 
# 
# #Data + Aesthetics + Geoms + Facets + Statistics
# # ... add a linear regression model here
#n3 <- n3 + geom_smooth(method="gam" ) 
# #Data + Aesthetics + Geoms + Facets + Statistics + Co-ordinates
# #n <- n + scale_y_discrete(limits=c("00000","120000"), breaks=seq(00000,120000,10000))
n3 <- n3 + scale_x_discrete()
n3 <- n3 + xlab("Years")
n3 <- n3 + ylab("MKT_TOTAL_VAL  ")
# 
# #Data + Aesthetics + Geoms + Facets + Statistics + Co-ordinates + Theme 
n3 <- n3 + theme(axis.text.x = element_text(angle=305))
# 
n3 <- n3 + ggtitle(title)
n3 <- n3 +facet_wrap( ~ zone)
print(n3)

## Split it
df_fin_1 <- df_final_mkt_val %>% filter((zone %in% c("CORE","non-Buffer")))

df_fin_2 <- df_final_mkt_val %>% filter((zone %in% c("CENTER","EDGE")))


n3 <- NULL
# Data Layer

title <- paste0("MKT TOTAL VALUE in different ZONEs CENTER and EDGE ")


n3 <-(ggplot)
# 
# # Data + Aesthetics Mapping 
#n3 <- ggplot(df_final_mkt_val,aes(y = mkt_total, x = factor(date), fill = factor(clst_num), color=factor(data_type)))
n3 <- ggplot(df_fin_2,aes(y = mkt_val, x = factor(date), fill = factor(clst_num), color=factor(data_type)))

#n3 <-   ggplot(df_final_mkt_val,aes(y = mkt_val, x = date ))
# 
# # Data + aes + Geometries
n3 <- n3 + geom_bar(stat="identity", width = 0.6) 
#n3 <- n3 + geom_line()
# 
# # Data + Aesthetic Mapping +  Geom + Facets
# #n <- n + facet_grid(. ~ Department.Title ) 
# 
# #Data + Aesthetics + Geoms + Facets + Statistics
# # ... add a linear regression model here
#n3 <- n3 + geom_smooth(method="gam" ) 
# #Data + Aesthetics + Geoms + Facets + Statistics + Co-ordinates
# #n <- n + scale_y_discrete(limits=c("00000","120000"), breaks=seq(00000,120000,10000))
n3 <- n3 + scale_x_discrete()
n3 <- n3 + xlab("Years")
n3 <- n3 + ylab("MKT_TOTAL_VAL  ")
# 
# #Data + Aesthetics + Geoms + Facets + Statistics + Co-ordinates + Theme 
n3 <- n3 + theme(axis.text.x = element_text(angle=305))
# 
n3 <- n3 + ggtitle(title)
n3 <- n3 +facet_wrap( ~ zone)
print(n3)

n3 <- NULL
# Data Layer
#title <- paste0("MKT TOTAL VALUE  in different ZONEs ")

title <- paste0("MKT TOTAL VALUE in different ZONEs CORE and non-Buffer ")


n3 <-(ggplot)
# 
# # Data + Aesthetics Mapping 
#n3 <- ggplot(df_final_mkt_val,aes(y = mkt_total, x = factor(date), fill = factor(clst_num), color=factor(data_type)))
n3 <- ggplot(df_fin_1,aes(y = mkt_val, x = factor(date), fill = factor(clst_num), color=factor(data_type)))

#n3 <-   ggplot(df_final_mkt_val,aes(y = mkt_val, x = date ))
# 
# # Data + aes + Geometries
n3 <- n3 + geom_bar(stat="identity", width = 0.6) 
#n3 <- n3 + geom_line()
# 
# # Data + Aesthetic Mapping +  Geom + Facets
# #n <- n + facet_grid(. ~ Department.Title ) 
# 
# #Data + Aesthetics + Geoms + Facets + Statistics
# # ... add a linear regression model here
#n3 <- n3 + geom_smooth(method="gam" ) 
# #Data + Aesthetics + Geoms + Facets + Statistics + Co-ordinates
# #n <- n + scale_y_discrete(limits=c("00000","120000"), breaks=seq(00000,120000,10000))
n3 <- n3 + scale_x_discrete()
n3 <- n3 + xlab("Years")
n3 <- n3 + ylab("MKT_TOTAL_VAL  ")
# 
# #Data + Aesthetics + Geoms + Facets + Statistics + Co-ordinates + Theme 
n3 <- n3 + theme(axis.text.x = element_text(angle=305))
# 
n3 <- n3 + ggtitle(title)
n3 <- n3 +facet_wrap( ~ zone)
print(n3)

# Identify clusters with growth in CENTER
cluster_max_val_parcels_CENTER <- remove_char_X(rownames(partCENTER100$var[[2]]))
# Identify clusters with decline in CENTER
cluster_declining_val_parcels_CENTER <- remove_char_X(rownames(partCENTER100$var[[1]]))

# Identify clusters with growth
cluster_max_val_parcels_EDGE <- remove_char_X(rownames(partEDGE100$var[[8]]))
# Identify clusters with growth
cluster_declining_val_parcels_EDGE <- remove_char_X(rownames(partEDGE100$var[[7]]))

# Cluster1  in Core  showing declinging trend
cluster_max_val_parcels_CORE <- remove_char_X(rownames(partCORE100$var[[1]] ))
# Cluster1  in Core  showing declinging trend
cluster_growing_val_parcels_CORE <- remove_char_X(rownames(partCORE100$var[[11]]))


cluster_max_val_parcels_NON_BFR <- remove_char_X(rownames(part1500$var[[5]]))


##############

# PLOTS on the MAP for EDGE

##############
library(ggmap)


###############
#Street Car Line : Convert shape files to data
###############
shp_file_streetcar <- readOGR('/Users/rajesh/Desktop/Coursera/SpringBoardGithub/StreetCar0719/streetcarbuffer_parcels/gis_shape_files/StreetCar/StreetCar.shp',
                              layer="StreetCar"     )
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/rajesh/Desktop/Coursera/SpringBoardGithub/StreetCar0719/streetcarbuffer_parcels/gis_shape_files/StreetCar/StreetCar.shp", layer: "StreetCar"
## with 25 features
## It has 11 fields
proj4string(shp_file_streetcar)
## [1] "+proj=lcc +lat_1=38.73333333333333 +lat_2=40.03333333333333 +lat_0=38 +lon_0=-82.5 +x_0=600000 +y_0=0 +datum=NAD83 +units=us-ft +no_defs +ellps=GRS80 +towgs84=0,0,0"
shp_file_streetcar <- spTransform(shp_file_streetcar, CRS("+proj=longlat +datum=WGS84"))

# convert to a data.frame for use with ggplot2/ggmap and plot
data <- fortify(shp_file_streetcar)

##############

# PLOTS on the MAP for EDGE

##############

myMap <- get_map(location="1208 Sycamore st, Cincinnati,OH", source="google", maptype="roadmap", crop=FALSE, zoom = 15)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=1208+Sycamore+st,+Cincinnati,OH&zoom=15&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=1208%20Sycamore%20st,%20Cincinnati,OH&sensor=false
title <- paste0("Density of Clusters of parcels with growing MKT_VAL in EDGE ")

# Growth 
df_cluster_max_val_EDGE <- as.data.frame(cluster_max_val_parcels_EDGE)
colnames(df_cluster_max_val_EDGE)<- c("PARCEL_ID")
df_cluster_max_val_EDGE <- semi_join(df_buffer_EDGE,df_cluster_max_val_EDGE, by="PARCEL_ID")

CinciCAGISDensityMap <- ggmap(myMap)
CinciCAGISDensityMap <- CinciCAGISDensityMap + stat_density2d(aes(x = as.numeric(cent_long), y = as.numeric(cent_lat), fill = ..level.., alpha = ..level..),  bins = 6, geom = "polygon", data = df_cluster_max_val_EDGE)
CinciCAGISDensityMap <- CinciCAGISDensityMap  + geom_point(data = df_cluster_max_val_EDGE, aes(x = as.numeric(cent_long), y = as.numeric(cent_lat)), alpha = 1)
CinciCAGISDensityMap <- CinciCAGISDensityMap + geom_polygon(aes(x = long, y = lat, group = group ), data = shp_file_streetcar,
                                                            alpha = 1.0, size = 1.0, fill = 'red', color = 'blue')                                           

CinciCAGISDensityMap <- CinciCAGISDensityMap + ggtitle(title)
print(CinciCAGISDensityMap)
## Warning: Removed 2 rows containing non-finite values (stat_density2d).
## Warning: Removed 2 rows containing missing values (geom_point).

#Decline
myMap <- get_map(location="1208 Sycamore st, Cincinnati,OH", source="google", maptype="roadmap", crop=FALSE, zoom = 15)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=1208+Sycamore+st,+Cincinnati,OH&zoom=15&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=1208%20Sycamore%20st,%20Cincinnati,OH&sensor=false
title <- paste0("Density of Clusters of parcels with declinging MKT_VAL in EDGE ")

df_cluster_declining_val_EDGE <- as.data.frame(cluster_declining_val_parcels_EDGE)
colnames(df_cluster_declining_val_EDGE)<- c("PARCEL_ID")
df_cluster_declining_val_EDGE <- semi_join(df_buffer_EDGE,df_cluster_declining_val_EDGE, by="PARCEL_ID")

CinciCAGISDensityMap <- ggmap(myMap)
CinciCAGISDensityMap <- CinciCAGISDensityMap + stat_density2d(aes(x = as.numeric(cent_long), y = as.numeric(cent_lat), fill = ..level.., alpha = ..level..),  bins = 6, geom = "polygon", data = df_cluster_declining_val_EDGE)
CinciCAGISDensityMap <- CinciCAGISDensityMap  + geom_point(data = df_cluster_declining_val_EDGE, aes(x = as.numeric(cent_long), y = as.numeric(cent_lat)), alpha = 1)
CinciCAGISDensityMap <- CinciCAGISDensityMap + geom_polygon(aes(x = long, y = lat, group = group ), data = shp_file_streetcar,
                                                            alpha = 1.0, size = 1.0, fill = 'red', color = 'blue')                                           

CinciCAGISDensityMap <- CinciCAGISDensityMap + ggtitle(title)
print(CinciCAGISDensityMap)
## Warning: Removed 20 rows containing non-finite values (stat_density2d).
## Warning: Removed 20 rows containing missing values (geom_point).

# plot on MAPS for CENTER

df_cluster_max_val_CENTER <- as.data.frame(cluster_max_val_parcels_CENTER)
colnames(df_cluster_max_val_CENTER)<- c("PARCEL_ID")
df_cluster_max_val_CENTER <- semi_join(df_buffer_CENTER,df_cluster_max_val_CENTER, by="PARCEL_ID")

myMap <- get_map(location="1208 Sycamore st, Cincinnati,OH", source="google", maptype="roadmap", crop=FALSE, zoom = 15)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=1208+Sycamore+st,+Cincinnati,OH&zoom=15&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=1208%20Sycamore%20st,%20Cincinnati,OH&sensor=false
title <- paste0("Density of Clusters of parcels with growingMKT_VAL in CENTER ")

CinciCAGISDensityMap <- ggmap(myMap)
CinciCAGISDensityMap <- CinciCAGISDensityMap + stat_density2d(aes(x = as.numeric(cent_long), y = as.numeric(cent_lat), fill = ..level.., alpha = ..level..), bins = 6, geom = "polygon", data = df_cluster_max_val_CENTER)
CinciCAGISDensityMap <- CinciCAGISDensityMap  + geom_point(data = df_cluster_max_val_CENTER, aes(x = as.numeric(cent_long), y = as.numeric(cent_lat)), alpha = 1)

CinciCAGISDensityMap <- CinciCAGISDensityMap + ggtitle(title)
CinciCAGISDensityMap <- CinciCAGISDensityMap + geom_polygon(aes(x = long, y = lat, group = group ), data = shp_file_streetcar,
                                                            alpha = 1.0, size = 1.0, fill = 'red', color = 'blue')
print(CinciCAGISDensityMap)
## Warning: Removed 1 rows containing non-finite values (stat_density2d).
## Warning: Removed 1 rows containing missing values (geom_point).

# Cluster 1 has declining values
df_cluster_declining_val_CENTER <- as.data.frame(cluster_declining_val_parcels_CENTER)
colnames(df_cluster_declining_val_CENTER)<- c("PARCEL_ID")
df_cluster_declining_val_CENTER <- semi_join(df_buffer_CENTER,df_cluster_declining_val_CENTER, by="PARCEL_ID")

myMap <- get_map(location="1208 Sycamore st, Cincinnati,OH", source="google", maptype="roadmap", crop=FALSE, zoom = 15)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=1208+Sycamore+st,+Cincinnati,OH&zoom=15&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=1208%20Sycamore%20st,%20Cincinnati,OH&sensor=false
title <- paste0("Density of Clusters of parcels with declining MKT_VAL in CENTER ")

CinciCAGISDensityMap <- ggmap(myMap)
CinciCAGISDensityMap <- CinciCAGISDensityMap + stat_density2d(aes(x = as.numeric(cent_long), y = as.numeric(cent_lat), fill = ..level.., alpha = ..level..), bins = 6, geom = "polygon", data = df_cluster_declining_val_CENTER)
CinciCAGISDensityMap <- CinciCAGISDensityMap  + geom_point(data = df_cluster_declining_val_CENTER, aes(x = as.numeric(cent_long), y = as.numeric(cent_lat)), alpha = 1)

CinciCAGISDensityMap <- CinciCAGISDensityMap + ggtitle(title)
CinciCAGISDensityMap <- CinciCAGISDensityMap + geom_polygon(aes(x = long, y = lat, group = group ), data = shp_file_streetcar,
                                                            alpha = 1.0, size = 1.0, fill = 'red', color = 'blue')
print(CinciCAGISDensityMap)
## Warning: Removed 18 rows containing non-finite values (stat_density2d).
## Warning: Removed 18 rows containing missing values (geom_point).

# plot on MAPS for CORE

df_cluster_max_val_CORE <- as.data.frame(cluster_max_val_parcels_CORE)
colnames(df_cluster_max_val_CORE)<- c("PARCEL_ID")
df_cluster_max_val_CORE <- semi_join(df_buffer_CORE,df_cluster_max_val_CORE, by="PARCEL_ID")
myMap <- get_map(location="1208 Sycamore st, Cincinnati,OH", source="google", maptype="roadmap", crop=FALSE, zoom = 15)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=1208+Sycamore+st,+Cincinnati,OH&zoom=15&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=1208%20Sycamore%20st,%20Cincinnati,OH&sensor=false
title <- paste0("Density of Clusters of parcels in  with declining MKT_VAL in CORE ")

CinciCAGISDensityMap <- ggmap(myMap)
CinciCAGISDensityMap <- CinciCAGISDensityMap + stat_density2d(aes(x = as.numeric(cent_long), y = as.numeric(cent_lat), fill = ..level.., alpha = ..level..),    bins = 6, geom = "polygon", data = df_cluster_max_val_CORE)
CinciCAGISDensityMap <- CinciCAGISDensityMap  + geom_point(data = df_cluster_max_val_CORE, aes(x = as.numeric(cent_long), y = as.numeric(cent_lat)), alpha = 1)
CinciCAGISDensityMap <- CinciCAGISDensityMap + ggtitle(title)
CinciCAGISDensityMap <- CinciCAGISDensityMap + geom_polygon(aes(x = long, y = lat, group = group ), data = shp_file_streetcar,
                                                            alpha = 1.0, size = 0.3, fill = 'red', color = 'blue')
print(CinciCAGISDensityMap)
## Warning: Removed 2 rows containing non-finite values (stat_density2d).
## Warning: Removed 2 rows containing missing values (geom_point).

df_cluster_growing_val_CORE <- as.data.frame(cluster_growing_val_parcels_CORE)
colnames(df_cluster_growing_val_CORE)<- c("PARCEL_ID")
df_cluster_max_val_CORE <- semi_join(df_buffer_CORE,df_cluster_growing_val_CORE, by="PARCEL_ID")
myMap <- get_map(location="1208 Sycamore st, Cincinnati,OH", source="google", maptype="roadmap", crop=FALSE, zoom = 15)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=1208+Sycamore+st,+Cincinnati,OH&zoom=15&size=640x640&scale=2&maptype=roadmap&language=en-EN&sensor=false
## Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=1208%20Sycamore%20st,%20Cincinnati,OH&sensor=false
title <- paste0("Density of Clusters of parcels with increasing MKT_VAL in CORE ")
CinciCAGISDensityMap <- ggmap(myMap)
CinciCAGISDensityMap <- CinciCAGISDensityMap + stat_density2d(aes(x = as.numeric(cent_long), y = as.numeric(cent_lat), fill = ..level.., alpha = ..level..),    bins = 6, geom = "polygon", data = df_cluster_max_val_CORE)
CinciCAGISDensityMap <- CinciCAGISDensityMap  + geom_point(data = df_cluster_max_val_CORE, aes(x = as.numeric(cent_long), y = as.numeric(cent_lat)), alpha = 1)
CinciCAGISDensityMap <- CinciCAGISDensityMap + ggtitle(title)
CinciCAGISDensityMap <- CinciCAGISDensityMap + geom_polygon(aes(x = long, y = lat, group = group ), data = shp_file_streetcar,
                                                            alpha = 1.0, size = 0.3, fill = 'red', color = 'blue')
print(CinciCAGISDensityMap)